package com.chixing.day12_date_factory;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateDemo {
    public static void main(String[] args) {
        Date date1;
        java.sql.Date date2;

        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT-8:00"));

        //创建指定日期
        LocalDate localDate1 = LocalDate.of(2000,6,12);
        System.out.println("指定的时间"+localDate1);
        //得到当前日期
        LocalDate localDate2 = LocalDate.now();
        System.out.println(localDate2);
        //获取日期信息
        System.out.println("年："+localDate2.getYear());
        System.out.println("月："+localDate2.getMonthValue());
        System.out.println("日："+localDate2.getDayOfMonth());
        System.out.println("星期："+localDate2.getDayOfWeek());
        System.out.println("一年中的第："+localDate2.getDayOfYear()+"天");

        //得到指定时间
        LocalTime localTime1 = LocalTime.of(9,18,32,8855);
        System.out.println("指定的时间"+localTime1);
        //得到当前时间
        LocalTime localTime2 = LocalTime.now();
        System.out.println("当前时间"+localTime2);
        //获得时间信息
        System.out.println("小时:"+localTime2.getHour());
        System.out.println("分钟:"+localTime2.getMinute());
        System.out.println("秒:"+localTime2.getSecond());
        System.out.println("纳秒:"+localTime2.getNano());

        LocalDateTime localDateTime1 = LocalDateTime.now();
        System.out.println("当前系统时间"+localDateTime1);
        //修该日期时间
        System.out.println("修改的年份："+localDateTime1.withYear(2025));
        System.out.println("修改的月份："+localDateTime1.withMonth(5));
        System.out.println("修改的日："+localDateTime1.withDayOfMonth(25));
        System.out.println("修改的小时："+localDateTime1.withHour(5));
        System.out.println("修改的分钟："+localDateTime1.withMinute(25));
        System.out.println("修改的秒："+localDateTime1.withSecond(20));
        System.out.println("修改的纳秒："+localDateTime1.withNano(225));

        //在当前对象加上或减去指定的时间
        System.out.println("5天后:"+localDateTime1.plusDays(5));
        System.out.println("10年后:"+localDateTime1.plusYears(10));
        System.out.println("20个月后:"+localDateTime1.plusMonths(20));

        System.out.println("20年前:"+localDateTime1.minusYears(20));
        System.out.println("5个月前:"+localDateTime1.minusMonths(5));
        System.out.println("100天前:"+localDateTime1.minusDays(100));

        //两个日期，可直接进行比较
        System.out.println(localDate1.equals(localDate2));
        System.out.println("1是否在2之后"+localDate1.isAfter(localDate2));
        System.out.println("1是否在2之前"+localDate1.isBefore(localDate2));

        //将当前日期时间格式化
        DateTimeFormatter formatter  = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //将日期时间格式化为字符串
        String dataTime = localDateTime1.format(formatter);
        System.out.println("格式化为字符串的时间日期"+dataTime);
        //将字符串解析为日期时间
        LocalDateTime parse = LocalDateTime.parse("2024-09-04 08:26:55",formatter);
        System.out.println("解析后的日期时间:"+parse);

        //时间戳
        Instant instant = Instant.now();
        System.out.println("当前时间戳:"+instant);
        System.out.println(instant.getNano());
        System.out.println(instant.getEpochSecond());
        System.out.println(instant.toEpochMilli());
        System.out.println(System.currentTimeMillis());

        Instant instant1 = Instant.ofEpochSecond(5);
        Instant instant2 = Instant.ofEpochMilli(5);
        System.out.println(instant1);
        System.out.println(instant2);

        //Duration计算时间的距离
        Duration duration = Duration.between(localTime1,localTime2);
        System.out.println("相差的天数："+duration.toDays());
        System.out.println("相差的小时数："+duration.toHours());
        System.out.println("相差的分钟："+duration.toMinutes());
        System.out.println("相差的秒数："+duration.toMillis());

        //Period计算日期的距离
        Period period = Period.between(localDate1,localDate2);
        System.out.println("相差的年:"+period.getYears());
        System.out.println("相差的月:"+period.getMonths());
        System.out.println("相差的日:"+period.getDays());



    }

    //时间校正器 TemporAdjuster
    public void testTemporalAdjuster() {
        LocalDateTime now = LocalDateTime.now();
        //得到下个月的第一天
        TemporalAdjuster temporalAdjuster = temporal ->{
            LocalDateTime dateTime = (LocalDateTime) temporal;
            LocalDateTime nextMonth = dateTime.plusMonths(1).withDayOfMonth(1);
            System.out.println("下个月的第一天 ： "+nextMonth);
            return nextMonth;
        };
        LocalDateTime nextMonth = now.with(temporalAdjuster);
        System.out.println("nextMonth :"+nextMonth);
    }
}

